package com.xuyang.fragment;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bookmanager.BookAddActivity;
import com.example.bookmanager.BookManageActivity;
import com.example.bookmanager.HistoryBorrowActivity;
import com.example.bookmanager.NowBorrowActivity;
import com.example.bookmanager.R;
import com.example.bookmanager.ResetPwActivity;
import com.xuyang.other.Flags;
import com.xuyang.other.LoginDialog;
import com.xuyang.other.LoginDialog.LoginSuccess;

public class UserFragment extends Fragment implements OnClickListener{
	private ImageView mImgUserIcon;
	private TextView mTxtUserState;
	private LinearLayout mLayoutBorrowNow,mLayoutBorrowHistory,mLayoutLogout;
	private LinearLayout mLayoutAdd,mLayoutReturn,mLayoutLoss,mLayoutReset;
	private LinearLayout mLayoutIsVip;
	
	private LoginDialog mLoginDialog;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.fragment_user, container, false);
		init(view);
		setClickListener();
		//如果flag小于等于0说明vip页面不显示
		if(Flags.Level<=0){
			mLayoutIsVip.setVisibility(View.GONE);
		}
		if(Flags.UserName!=""){
			mTxtUserState.setText("已登录");
		}
		return view;
	}

	private void init(View view) {
		mImgUserIcon=(ImageView) view.findViewById(R.id.img_user_icon);
		mTxtUserState=(TextView) view.findViewById(R.id.txt_user_state);
		mLayoutBorrowNow=(LinearLayout) view.findViewById(R.id.layout_user_borrow_now);
		mLayoutBorrowHistory=(LinearLayout) view.findViewById(R.id.layout_user_borrow_history);
		mLayoutLogout=(LinearLayout) view.findViewById(R.id.layout_user_logout);
		mLayoutAdd=(LinearLayout) view.findViewById(R.id.layout_user_addbook);
		mLayoutReturn=(LinearLayout) view.findViewById(R.id.layout_user_returnbook);
		mLayoutLoss=(LinearLayout) view.findViewById(R.id.layout_user_lossbook);
		mLayoutReset=(LinearLayout) view.findViewById(R.id.layout_user_resetpw);
		mLayoutIsVip=(LinearLayout) view.findViewById(R.id.layout_user_vip);
		mLoginDialog=new LoginDialog(getActivity());
	}
	private void setClickListener() {
		mLayoutBorrowNow.setOnClickListener(this);
		mImgUserIcon.setOnClickListener(this);
		mTxtUserState.setOnClickListener(this);
		mLayoutBorrowHistory.setOnClickListener(this);
		mLayoutLogout.setOnClickListener(this);
		mLayoutAdd.setOnClickListener(this);
		mLayoutReturn.setOnClickListener(this);
		mLayoutLoss.setOnClickListener(this);
		mLayoutReset.setOnClickListener(this);
		mLoginDialog.setOnLoginSuccess(new LoginSuccess() {
			@Override
			public void loginSuccess() {
				mTxtUserState.setText("已登录");
				if (Flags.Level!=0) {
					mLayoutIsVip.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_user_icon:
		case R.id.txt_user_state:
			//如果用户名不等于空，表面已经登录跳出账户管理页面
			if(Flags.UserName!=""){
				
			}else {
				mLoginDialog.show(v);
			}
			
			return;
		}
		if(Flags.UserName==""){
			Toast.makeText(getActivity(), "清先登录", 0).show();
			return ;
		}
		Intent intent;
		switch (v.getId()) {
		case R.id.layout_user_borrow_now:
			intent=new Intent(getActivity(),NowBorrowActivity.class);
			startActivity(intent);
			break;
		case R.id.layout_user_borrow_history:
			intent=new Intent(getActivity(),HistoryBorrowActivity.class);
			startActivity(intent);
			break;
		case R.id.layout_user_logout:
			if(Flags.UserName!=""){
				new AlertDialog.Builder(getActivity())
				.setTitle("确定退出?")
				.setMessage("是否确定退出当前登录？")
				.setPositiveButton("退出", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Flags.UserName="";
						Flags.Level=-2;
						mLayoutIsVip.setVisibility(View.GONE);
						mTxtUserState.setText("未登录");
						Toast.makeText(getActivity(), "退出成功", 0).show();
					}
				})
				.setNegativeButton("取消", null)
				.show();
			}
			break;
		case R.id.layout_user_addbook:
			intent=new Intent(getActivity(),BookAddActivity.class);
			startActivity(intent);
			break;
		case R.id.layout_user_returnbook:
			intent=new Intent(getActivity(), BookManageActivity.class);
			intent.setFlags(2);
			startActivity(intent);
			break;
		case R.id.layout_user_lossbook:
			intent=new Intent(getActivity(), BookManageActivity.class);
			intent.setFlags(3);
			startActivity(intent);
			break;
		case R.id.layout_user_resetpw:
			intent=new Intent(getActivity(), ResetPwActivity.class);
			startActivity(intent);
			break;
		}
	}
}
